home *** CD-ROM | disk | FTP | other *** search
/ Champak 43 / Vol 43.iso / games / phit.swf / scripts / __Packages / CCollisionResolverBucketSort.as < prev    next >
Encoding:
Text File  |  2007-07-13  |  9.9 KB  |  274 lines

  1. class CCollisionResolverBucketSort implements ICollisionResolver
  2. {
  3.    var m_matrixWidth;
  4.    var m_matrixHeight;
  5.    var m_stageUpperLeftCorner;
  6.    var m_stagePixelsPerMatrixWidth;
  7.    var m_stagePixelsPerMatrixHeight;
  8.    var m_bucketMatrix;
  9.    var m_arrActiveBuckets;
  10.    function CCollisionResolverBucketSort(width, height, stageWidth, stageHeight, stageLeft, stageTop)
  11.    {
  12.       this.m_matrixWidth = width;
  13.       this.m_matrixHeight = height;
  14.       this.m_stageUpperLeftCorner = new Vector2D(stageLeft,stageTop);
  15.       this.m_stagePixelsPerMatrixWidth = stageWidth / this.m_matrixWidth;
  16.       this.m_stagePixelsPerMatrixHeight = stageHeight / this.m_matrixHeight;
  17.       this.m_bucketMatrix = new Array(this.m_matrixWidth * this.m_matrixHeight);
  18.       var _loc3_ = 0;
  19.       while(_loc3_ < this.m_matrixWidth)
  20.       {
  21.          var _loc2_ = 0;
  22.          while(_loc2_ < this.m_matrixHeight)
  23.          {
  24.             this.m_bucketMatrix[_loc3_ + _loc2_ * this.m_matrixWidth] = new CCollisionBucket(_loc3_,_loc2_);
  25.             _loc2_ = _loc2_ + 1;
  26.          }
  27.          _loc3_ = _loc3_ + 1;
  28.       }
  29.       this.m_arrActiveBuckets = new Array();
  30.       this.CreateDebugDisplay();
  31.    }
  32.    function UpdateStagePosition(stageLeft, stageTop, stageWidth, stageHeight)
  33.    {
  34.       this.m_stageUpperLeftCorner._x = stageLeft;
  35.       this.m_stageUpperLeftCorner._y = stageTop;
  36.       _global._bucketResolverDebugDisplay._x = stageLeft;
  37.       _global._bucketResolverDebugDisplay._y = stageTop;
  38.    }
  39.    function get _matrixWidth()
  40.    {
  41.       return this.m_matrixWidth;
  42.    }
  43.    function get _matrixHeight()
  44.    {
  45.       return this.m_matrixHeight;
  46.    }
  47.    function CreateDebugDisplay()
  48.    {
  49.       if(!_global._bucketResolverDebugDisplay)
  50.       {
  51.          if(!_global._bucketResolverDebugDisplayRoot)
  52.          {
  53.             _global._bucketResolverDebugDisplayRoot = _root;
  54.          }
  55.          _global._bucketResolverDebugDisplay = _global._bucketResolverDebugDisplayRoot.createEmptyMovieClip("_bucketResolverDebugDisplay",_global._bucketResolverDebugDisplayRoot.getNextHighestDepth());
  56.       }
  57.       var _loc5_ = 0;
  58.       while(_loc5_ < this.m_matrixWidth)
  59.       {
  60.          var _loc4_ = 0;
  61.          while(_loc4_ < this.m_matrixHeight)
  62.          {
  63.             _global._bucketResolverDebugDisplay.attachMovie("Bucket Debug Square","_bucketDebug" + (_loc5_ + _loc4_ * this.m_matrixWidth),_global._bucketResolverDebugDisplay.getNextHighestDepth(),{_x:this.m_stageUpperLeftCorner._x + _loc5_ * this.m_stagePixelsPerMatrixWidth,_y:this.m_stageUpperLeftCorner._y + _loc4_ * this.m_stagePixelsPerMatrixHeight,_xscale:this.m_stagePixelsPerMatrixWidth,_yscale:this.m_stagePixelsPerMatrixHeight});
  64.             _loc4_ = _loc4_ + 1;
  65.          }
  66.          _loc5_ = _loc5_ + 1;
  67.       }
  68.    }
  69.    function GetMatrixCoordinatesFromLocation(loc)
  70.    {
  71.       return new Vector2D(int((loc._x - this.m_stageUpperLeftCorner._x) / this.m_stagePixelsPerMatrixWidth),int((loc._y - this.m_stageUpperLeftCorner._y) / this.m_stagePixelsPerMatrixHeight));
  72.    }
  73.    function ResolveCollisions(bodies)
  74.    {
  75.       this.m_arrActiveBuckets.splice(0);
  76.       this.UpdateBodyBuckets(bodies);
  77.       this.ResolveCollisionWithinBuckets();
  78.       if(_global._bucketResolverDebugDisplay._visible)
  79.       {
  80.          this.UpdateBucketDebugDisplay();
  81.       }
  82.    }
  83.    function UpdateBodyBuckets(bodies)
  84.    {
  85.       var _loc3_ = 0;
  86.       while(_loc3_ < bodies.length)
  87.       {
  88.          var _loc2_ = bodies[_loc3_];
  89.          if(_loc2_._doesCollide)
  90.          {
  91.             if(_loc2_._didMove || _loc2_._nUpdates == 1)
  92.             {
  93.                _loc2_.VacatePriorCollisionBuckets();
  94.                this.AddBodyToOverlappingBuckets(_loc2_);
  95.             }
  96.          }
  97.          else
  98.          {
  99.             _loc2_.VacatePriorCollisionBuckets();
  100.          }
  101.          _loc3_ = _loc3_ + 1;
  102.       }
  103.    }
  104.    function UpdateBucketDebugDisplay()
  105.    {
  106.       var _loc6_ = 0;
  107.       while(_loc6_ < this.m_matrixWidth)
  108.       {
  109.          var _loc3_ = 0;
  110.          while(_loc3_ < this.m_matrixHeight)
  111.          {
  112.             var _loc8_ = _global._bucketResolverDebugDisplay["_bucketDebug" + (_loc6_ + _loc3_ * this.m_matrixWidth)];
  113.             var _loc7_ = new Color(_loc8_);
  114.             _loc7_.setRGB(0);
  115.             var _loc4_ = this.m_bucketMatrix[_loc6_ + _loc3_ * this.m_matrixWidth];
  116.             if(_loc4_.m_arrOverlappingBodies.length > 0)
  117.             {
  118.                var _loc5_ = 65280;
  119.                if(_loc4_.m_arrOverlappingBodies.length > 1)
  120.                {
  121.                   _loc5_ = 16711680;
  122.                }
  123.                _loc7_.setRGB(_loc5_);
  124.             }
  125.             _loc3_ = _loc3_ + 1;
  126.          }
  127.          _loc6_ = _loc6_ + 1;
  128.       }
  129.    }
  130.    function AddBodyToOverlappingBuckets(body)
  131.    {
  132.       var _loc5_ = this.GetMatrixCoordinatesFromLocation(body._topLeftCorner);
  133.       var _loc6_ = this.GetMatrixCoordinatesFromLocation(body._bottomRightCorner);
  134.       var _loc4_ = _loc5_._x;
  135.       while(_loc4_ <= _loc6_._x)
  136.       {
  137.          if(!(_loc4_ < 0 || _loc4_ >= this.m_matrixWidth))
  138.          {
  139.             var _loc2_ = _loc5_._y;
  140.             while(_loc2_ <= _loc6_._y)
  141.             {
  142.                if(!(_loc2_ < 0 || _loc2_ >= this.m_matrixHeight))
  143.                {
  144.                   var _loc3_ = this.m_bucketMatrix[int(_loc4_ + _loc2_ * this.m_matrixWidth)];
  145.                   _loc3_.AddBody(body);
  146.                   if(_loc3_.m_arrOverlappingBodies.length >= 2)
  147.                   {
  148.                      this.NoteActiveBucket(_loc3_);
  149.                   }
  150.                }
  151.                _loc2_ = _loc2_ + 1;
  152.             }
  153.          }
  154.          _loc4_ = _loc4_ + 1;
  155.       }
  156.    }
  157.    function NoteActiveBucket(bucket)
  158.    {
  159.       var _loc3_ = this.m_arrActiveBuckets.length;
  160.       var _loc2_ = 0;
  161.       while(_loc2_ < _loc3_)
  162.       {
  163.          if(this.m_arrActiveBuckets[_loc2_] == bucket)
  164.          {
  165.             return undefined;
  166.          }
  167.          _loc2_ = _loc2_ + 1;
  168.       }
  169.       this.m_arrActiveBuckets.push(bucket);
  170.    }
  171.    function ResolveCollisionWithinBuckets()
  172.    {
  173.       var _loc8_ = new Array();
  174.       var _loc11_ = this.m_arrActiveBuckets.length;
  175.       var _loc10_ = 0;
  176.       while(_loc10_ < _loc11_)
  177.       {
  178.          var _loc5_ = this.m_arrActiveBuckets[_loc10_].m_arrOverlappingBodies;
  179.          var _loc9_ = 0;
  180.          while(_loc9_ < _loc5_.length - 1)
  181.          {
  182.             var _loc4_ = _loc5_[_loc9_];
  183.             if(!_loc4_)
  184.             {
  185.                _loc5_.splice(_loc9_,1);
  186.                _loc9_ = _loc9_ - 1;
  187.             }
  188.             else
  189.             {
  190.                var _loc2_ = _loc9_ + 1;
  191.                while(_loc2_ < _loc5_.length)
  192.                {
  193.                   var _loc3_ = _loc5_[_loc2_];
  194.                   if(!_loc3_)
  195.                   {
  196.                      _loc5_.splice(_loc2_,1);
  197.                      _loc2_ = _loc2_ - 1;
  198.                   }
  199.                   else
  200.                   {
  201.                      FreshDebug.Assert(_loc4_ != _loc3_,"CCollisionResolverBucketSort.ResolveCollisionWithinBuckets(): bodyI [ " + _loc4_ + " ] != bodyJ");
  202.                      if(!this.HasCollisionPair(_loc4_,_loc3_,_loc8_))
  203.                      {
  204.                         var _loc6_ = new Object();
  205.                         _loc6_._body1 = _loc4_;
  206.                         _loc6_._body2 = _loc3_;
  207.                         _loc8_.push(_loc6_);
  208.                      }
  209.                   }
  210.                   _loc2_ = _loc2_ + 1;
  211.                }
  212.             }
  213.             _loc9_ = _loc9_ + 1;
  214.          }
  215.          _loc10_ = _loc10_ + 1;
  216.       }
  217.       _loc9_ = 0;
  218.       while(_loc9_ < _loc8_.length)
  219.       {
  220.          var _loc7_ = _loc8_[_loc9_]._body1.DoesCollideWith(_loc8_[_loc9_]._body2);
  221.          if(_loc7_)
  222.          {
  223.             if(!_loc7_.m_body1.DoesSuppressCentralizedCollisionResolution() && !_loc7_.m_body2.DoesSuppressCentralizedCollisionResolution())
  224.             {
  225.                this.ResolveCollision(_loc7_);
  226.             }
  227.             _loc7_.m_body1.NotifyCollision(_loc7_);
  228.             _loc7_.m_body2.NotifyCollision(_loc7_);
  229.          }
  230.          _loc9_ = _loc9_ + 1;
  231.       }
  232.    }
  233.    function HasCollisionPair(bodyI, bodyJ, arrPairs)
  234.    {
  235.       var _loc2_ = 0;
  236.       while(_loc2_ < arrPairs.length)
  237.       {
  238.          var _loc1_ = arrPairs[_loc2_];
  239.          if(_loc1_._body1 == bodyI && _loc1_._body2 == bodyJ || _loc1_._body1 == bodyJ && _loc1_._body2 == bodyI)
  240.          {
  241.             return true;
  242.          }
  243.          _loc2_ = _loc2_ + 1;
  244.       }
  245.       return false;
  246.    }
  247.    function ResolveCollision(collisionInfo)
  248.    {
  249.       var _loc2_ = collisionInfo.m_body1.m_restitution * collisionInfo.m_body2.m_restitution;
  250.       var _loc6_ = collisionInfo.m_body1.m_mass / (collisionInfo.m_body1.m_mass + collisionInfo.m_body2.m_mass);
  251.       var _loc13_ = collisionInfo.m_body2.m_mass / (collisionInfo.m_body1.m_mass + collisionInfo.m_body2.m_mass);
  252.       var _loc5_ = collisionInfo.m_normal;
  253.       var _loc8_ = _loc5_.GetInverse();
  254.       var _loc10_ = collisionInfo.m_body1._velocity;
  255.       var _loc9_ = collisionInfo.m_body2._velocity;
  256.       var _loc3_ = _loc5_.GetMultiplyScalar(_loc5_.DotProduct(_loc10_));
  257.       var _loc11_ = _loc10_.GetSubtract(_loc3_);
  258.       var _loc4_ = _loc8_.GetMultiplyScalar(_loc8_.DotProduct(_loc9_));
  259.       var _loc12_ = _loc9_.GetSubtract(_loc4_);
  260.       var _loc7_ = _loc11_.GetSubtract(_loc3_.GetMultiplyScalar(_loc6_ * _loc2_).GetSubtract(_loc4_).GetMultiplyScalar(_loc13_ * _loc2_));
  261.       if(collisionInfo.m_body2.m_mass <= 0)
  262.       {
  263.          _loc7_ = _loc11_.GetSubtract(_loc3_.GetMultiplyScalar(_loc2_));
  264.       }
  265.       var _loc14_ = _loc12_.GetSubtract(_loc4_.GetMultiplyScalar(_loc13_ * _loc2_).GetSubtract(_loc3_).GetMultiplyScalar(_loc6_ * _loc2_));
  266.       if(collisionInfo.m_body1.m_mass <= 0)
  267.       {
  268.          _loc14_ = _loc12_.GetSubtract(_loc4_.GetMultiplyScalar(_loc2_));
  269.       }
  270.       collisionInfo.m_body1.ApplyImpulse(_loc7_);
  271.       collisionInfo.m_body2.ApplyImpulse(_loc14_);
  272.    }
  273. }
  274.